---------------------------------------------------------------
--Map.lua                                                      -
--Functions for handling rendering, editing and displaying maps-
---------------------------------------------------------------
require 'gamebase.tile'
require 'gamebase.button'

--Data Structure Flags
MapDataFlags = {}

MapDataFlags.Header = 5490

MapDataFlags.SOTile = 5500
MapDataFlags.EOTile = 5510

MapDataFlags.SODimProps = 5520
MapDataFlags.EODimProps = 5530
----------------------------------

--Map Structure Layout
Map = {}

Map.tileImagePath = "content/tiles/null.png"
Map.tileData = {}

Map.Width = 50
Map.Height = 38
Map.tileSize = 16
Map.tileSheetW = 7
Map.tileSheetH = 15
Map.tileSheetFrame = {}
-----------------------------------

--Map Editor Properties
Map.Editor = {}

Map.Editor.isEnabled = true
Map.Editor.curTileX = 0
Map.Editor.curTileY = 0
Map.Editor.curTileProperty = TileProperties.isPassive

Map.Editor.displayTileset = true;
Map.Editor.displayProperties = false;
Map.Editor.placeTiles = true;
Map.Editor.placeProperties = false;
---------------------------------------

--TileWindow Properties
Map.Editor.TileWindow = {}

Map.Editor.TileWindow.posX = love.graphics.getWidth() - 150
Map.Editor.TileWindow.posY = 5

Map.Editor.TileWindow.padding = 10
Map.Editor.TileWindow.amtRows = 3
Map.Editor.TileWindow.perRow = 7
Map.Editor.TileWindow.lowBounds = 0
Map.Editor.TileWindow.currentColumn = 0
Map.Editor.TileWindow.ColumnsToDraw = 5
Map.Editor.TileWindow.MaxColumns = 10
Map.Editor.TileWindow.tileLimit = 20*15


--PropWindow Properties


------------------------------------------------------------------


------------------------------------

function Map:Init(_w, _h, _tsize, _path)
	self.Width = _w
	self.Height = _h
	self.tileSize = _tsize
	self.tileImagePath = _path
	
	--init the tile data array
	for i = 0, self.Width do
		self.tileData[i] = {}
			
		for j = 0, self.Height do
			self.tileData[i][j] = Tile:Init()
		end
	end

	--load the tileset image
	self.image = love.graphics.newImage(self.tileImagePath)
	
	--init the tilesheet frame array
	for i = 0, self.tileSheetW do
		self.tileSheetFrame[i] = {}
		for j = 0, self.tileSheetH do
			self.tileSheetFrame[i][j] = love.graphics.newQuad( self.tileSize*i, self.tileSize*j, 
				self.tileSize, self.tileSize, self.image:getWidth(), self.image:getHeight() )
		end
	end	
	
	--init the map editor buttons
	--todo: this code looks like crap. I've been wanting to just be able to set everything
	--from a function call inside of Button.lua
	self.Editor.TileWindow.buttonTileGrid = Button:Init(self.Editor.TileWindow.posX+(self.Editor.TileWindow.padding),self.Editor.TileWindow.posY+(self.Editor.TileWindow.padding),self.Editor.TileWindow.perRow * self.tileSize,self.Editor.TileWindow.ColumnsToDraw*self.tileSize, "TileGrid")
	
	self.Editor.TileWindow.buttonTileUp = Button:Init(self.Editor.TileWindow.posX+(self.Editor.TileWindow.padding),115,32,20,"^")
	self.Editor.TileWindow.buttonTileDown = Button:Init(self.Editor.TileWindow.posX+(self.Editor.TileWindow.padding)+ 40,115,32,20,"V")
	
	self.Editor.TileWindow.buttonSave = Button:Init(self.Editor.TileWindow.posX+self.Editor.TileWindow.padding+65,500,60,32,"Save")
	
	self.Editor.TileWindow.buttonLoad = Button:Init(self.Editor.TileWindow.posX+self.Editor.TileWindow.padding,500,60,32,"Load")
	
end

function Map:Update()
	local placeTile = true
--Use for animating tiles/other things that need updating on a map
--NOT A PRIORITY
	if (self.Editor.isEnabled) then
		-- tile window controls
		if love.keyboard.wasReleased("t") then
			if (Map.Editor.displayTileset == false) then
				Map.Editor.displayTileset = true
				Map.Editor.placeTiles = true
				Map.Editor.displayProperties = false
			elseif (Map.Editor.displayTileset == true) then
				Map.Editor.displayTileset = false
				Map.Editor.placeTiles = false
			end
		end
	--scroll tilegrid up
		if (self.Editor.TileWindow.buttonTileUp:Update()) then
			if (self.Editor.TileWindow.currentColumn > 0) then
				self.Editor.TileWindow.currentColumn = self.Editor.TileWindow.currentColumn - self.Editor.TileWindow.ColumnsToDraw
			end
			placeTile = false
		end
	--scroll tile grid down
		if (self.Editor.TileWindow.buttonTileDown:Update()) then
			if (self.Editor.TileWindow.currentColumn < self.Editor.TileWindow.MaxColumns) then
				self.Editor.TileWindow.currentColumn = self.Editor.TileWindow.currentColumn + self.Editor.TileWindow.ColumnsToDraw 
			end
			placeTile = false
		end
		if (self.Editor.TileWindow.buttonTileGrid:Update()) then
			local _tilex = love.mouse.getX() - self.Editor.TileWindow.posX+(self.Editor.TileWindow.padding)
			local _tiley = love.mouse.getY() - self.Editor.TileWindow.posY+(self.Editor.TileWindow.padding)
			
			_tiley = _tiley + (self.Editor.TileWindow.currentColumn * 16)
			
			_tilex = math.floor(_tilex / self.tileSize)
			_tiley = math.floor(_tiley / self.tileSize)
			
			self.Editor.curTileX = _tilex - 1
			self.Editor.curTileY = _tiley - 1
			
			placeTile = false
		end
		if (placeTile and love.mouse.isDown('l')) then
			local mx = math.floor(love.mouse.getX()/self.tileSize)
			local my = math.floor(love.mouse.getY()/self.tileSize)
			self.tileData[mx][my].X = self.Editor.curTileX
			self.tileData[mx][my].Y = self.Editor.curTileY
		end
	--save the map
	--load the map
		-- property window controls
	end
end

function Map:Draw()
	--todo: later add support for the camera
	for i = 0, self.Width do
		for j = 0, self.Height do
			love.graphics.drawq( self.image, self.tileSheetFrame[self.tileData[i][j].X][self.tileData[i][j].Y], i*self.tileSize, j*self.tileSize)
		end
	end
end

function Map:ChangeProperty(_x,_y)
end

function Map:DrawTileWindow()
	local isDrawing = true
	
	local _padding = self.Editor.TileWindow.padding
	local _x = 0
	local _y = 0
	
	local _curColumn = self.Editor.TileWindow.currentColumn 
	local _perRow = self.Editor.TileWindow.perRow - 1
	local _colPos = 0
	local _tileCount = 0
	local _colMax = self.Editor.TileWindow.ColumnsToDraw
	
	--Draw the background rect
		love.graphics.setColor(104, 136, 248)
		love.graphics.rectangle("fill",Map.Editor.TileWindow.posX-3, self.tileSize+Map.Editor.TileWindow.posY-20, 150, 600)
		
		love.graphics.setColor(255,255,255)
		love.graphics.rectangle("line",Map.Editor.TileWindow.posX-3, self.tileSize+Map.Editor.TileWindow.posY-20, 150, 600)
		
	--Draw text, labels, buttons
	self.Editor.TileWindow.buttonTileGrid:Draw()
	self.Editor.TileWindow.buttonTileUp:Draw()
	self.Editor.TileWindow.buttonTileDown:Draw()
	
	self.Editor.TileWindow.buttonSave:Draw()
	self.Editor.TileWindow.buttonLoad:Draw()
	--Draw the tilesheet
	while (isDrawing) do
		if (self.Editor.curTileX == _x and self.Editor.curTileY == _colPos+_curColumn) then
			love.graphics.setColor(200, 136, 248)
			love.graphics.drawq(self.image,self.tileSheetFrame[_x][_colPos+_curColumn], _padding + _x*self.tileSize+Map.Editor.TileWindow.posX, _padding + _colPos*self.tileSize+Map.Editor.TileWindow.posY)
		else
			love.graphics.setColor(255, 255, 255)
			love.graphics.drawq(self.image,self.tileSheetFrame[_x][_colPos+_curColumn], _padding + _x*self.tileSize+Map.Editor.TileWindow.posX, _padding + _colPos*self.tileSize+Map.Editor.TileWindow.posY)
		end
		_x = _x + 1
		if (_x > _perRow)  then
			_colPos = _colPos + 1
			_x = 0
		end
		
		_tileCount = _tileCount + 1
		
		if (_tileCount == self.Editor.TileWindow.tileLimit) or (_colPos == _colMax) then
			isDrawing = false
		end
		
		
	end

end

function Map:GetTileQuad(x,y)

	return self.tileSheetFrame[x][y]
end

function Map:DrawProperty()
end

function Map:DrawEditorUI()
	if (Map.Editor.displayTileset == true) then
		self:DrawTileWindow()
	end
end

function Map:SetTileAtPx(_x,_y)
end

function Map:Load()
end

function Map:Save()
--https://love2d.org/wiki/love.filesystem.write
end